package com.zkh.myutils.database.helper;

import java.util.ArrayList;
import java.util.List;

import com.zkh.myutils.database.enums.Symbol;

/**
 * <p>条件块对象，表示一个逻辑块。如有以下查询语句：</p>
 * <p><b>select * from table where a='1' and b&lt;10 and (c&lt;5 or c&gt;100) or d in ('11', '22')</b></p>
 * 在上面where条件中，条件块共有四个，分别是：<ol>
 * <li>a='1'</li>
 * <li>and b<10</li>
 * <li>and (c&lt;5 or c>100)</li>
 * <li>or d in ('11', '22')</li></ol>
 * <p>而在 <b>and (c&lt;5 or c>100)</b> 中，又可以拆分为 <b>c&lt;5</b> 和 <b>or c>100</b> 两个子条件块。</p>
 * <p>可以看出，条件块由两个部分构成，一个是逻辑连接符and和or，一个就是条件逻辑。</p>
 * @author zkh
 */
public class ConditionPart {
	// 与前一个条件块之间的逻辑（此处仅限枚举值AND和OR）
	private Symbol logic;
	//条件逻辑
	private Relation relation;
	//子条件块
	private List<ConditionPart> childrenPart;
	
	/**
	 * 构造方法
	 */
	public ConditionPart() {
	}
	/**
	 * 构造方法
	 * @param logic 逻辑连接符
	 */
	public ConditionPart(Symbol logic) {
		this.logic = logic;
	}
	/**
	 * 构造方法
	 * @param relation 条件逻辑
	 */
	public ConditionPart(Relation relation) {
		this.relation = relation;
	}
	/**
	 * 构造方法
	 * @param relation 条件逻辑
	 * @param logic 逻辑连接符
	 */
	public ConditionPart(Relation relation, Symbol logic) {
		this.relation = relation;
		this.logic = logic;
	}
	/**
	 * 构造方法
	 * @param childrenPart 子条件块
	 */
	public ConditionPart(List<ConditionPart> childrenPart) {
		this.childrenPart = childrenPart;
	}
	/**
	 * 构造方法
	 * @param childrenPart 子条件块
	 * @param logic 逻辑连接符
	 */
	public ConditionPart(List<ConditionPart> childrenPart, Symbol logic) {
		this.childrenPart = childrenPart;
		this.logic = logic;
	}

	public Symbol getLogic() {
		return logic;
	}
	public void setLogic(Symbol logic) {
		this.logic = logic;
	}
	public Relation getRelation() {
		return relation;
	}
	public void setRelation(Relation relation) {
		this.relation = relation;
	}
	public List<ConditionPart> getChildrenPart() {
		return childrenPart;
	}
	public void setChildrenPart(List<ConditionPart> childrenPart) {
		this.childrenPart = childrenPart;
	}
	/**
	 * 追加子条件块
	 * @param conditionPart 条件块
	 */
	public void appendChildrenPart(ConditionPart conditionPart) {
		//容器为空，先初始化
		if(childrenPart==null) {
			childrenPart = new ArrayList<>();
		}
		childrenPart.add(conditionPart);
	}
}
